package controller;

import model.Discipline;
import model.PersistenceType;
import model.ReturnDTO;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import service.DisciplineService;

/** 
 *
 * @author Moraes
 */
public class DisciplineController extends SimpleFormController {

    private DisciplineService disciplineService;

    public void setDisciplineService(DisciplineService disciplineService) {
        this.disciplineService = disciplineService;
    }

    public DisciplineController() {
        setCommandClass(Discipline.class);
        setCommandName("disciplineCommand");
        setSuccessView("successView");
        setFormView("disciplineView");
    }
    
//    @Override
//    protected void doSubmitAction(Object command) throws Exception {
//        throw new UnsupportedOperationException("Not yet implemented");
//    }

    @Override
    protected ModelAndView onSubmit(Object command) throws Exception {
        Discipline discipline = (Discipline) command;
        ReturnDTO returnDTO = new ReturnDTO();

        returnDTO = disciplineService.PersistDiscipline(discipline, PersistenceType.INSERT);
        if (returnDTO.result) {
            ModelAndView mv = new ModelAndView(getSuccessView());
            mv.addObject("successMessage", "Cadastro realizado com sucesso");
            return mv;
        } else {
            ModelAndView mv = new ModelAndView(getFormView());
            mv.addObject("errorMessage", "Ocorreu um Erro." + returnDTO.errorMessage);
            return mv;
        }
    }


}